<?php
/**
 * 巡展基金项目模型
 * @author qianxun
 *
 */
class xunProjectModel extends MY_Model{
    function xunProjectModel(){
        parent::MY_Model();
        $this->table = 'XunProject';
    }
    /**
     * 更新父级金额
     * {@inheritDoc}
     * @see MY_Model::updateParentMoneys()
     */
    function updateParentMoneys($parentId){
        $this->load->model('xunProcessModel');
        $this->xunProcessModel->updateMoney($parentId);
    }
    /**
     * 获取父级金额ID
     * {@inheritDoc}
     * @see MY_Model::getParentId()
     */
    function getParentId($data = array()){
        return isset($data['procId']) && $data['procId'] ? (int)$data['procId'] : 0;
    }
    /**
     * 包装数据
     * {@inheritDoc}
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        $this->load->model('carLevelModel');
        //活动类型配置
        $carLevelList = $this->carLevelModel->getCacheData('carLevelList');
        //角色列表
        $roleList = $this->config->item('roleList');
        //审核结果
        $auditRs = $this->config->item('auditRs');
        foreach($list as $k => $v){
            //反序列化凭证数据
            $v['budgetVoucher'] = unserialize($v['budgetVoucher']);
            $v['settleVoucher'] = unserialize($v['settleVoucher']);
            //级别名称
            $v['levelIdStr'] = isset($carLevelList[$v['levelId']]) ? $carLevelList[$v['levelId']]['name'] : '';
            //预算审核状态
            $v['budgetAuditStr'] = isset($roleList[$v['budgetAuditRole']]) ? $roleList[$v['budgetAuditRole']].'已审核' : '';
            if ($v['budgetAuditStr']){
                $v['budgetAuditStr'] .= isset($auditRs[$v['budgetAuditRs']]) ? ','.$auditRs[$v['budgetAuditRs']] : '';
            }else{
                $v['budgetAuditStr'] = isset($auditRs[$v['budgetAuditRs']]) ? $auditRs[$v['budgetAuditRs']] : '';
            }
            //审核状态(结算审核状态)
            $v['settleAuditRs'] = (int)$v['settleAuditRsHead'] ? $v['settleAuditRsHead'] : $v['settleAuditRsArea'];
            //区域结算审核状态
            if ($v['settleAuditRole'] == 2){
                $v['settleAuditAreaStr'] = isset($roleList[$v['settleAuditRole']]) ? $roleList[$v['settleAuditRole']].'已审核' : '';
            }
            if ($v['settleAuditAreaStr']){
                $v['settleAuditAreaStr'] .= isset($auditRs[$v['settleAuditRsArea']]) ? ','.$auditRs[$v['settleAuditRsArea']] : '';
            }else{
                $v['settleAuditAreaStr'] = isset($auditRs[$v['settleAuditRsArea']]) ? $auditRs[$v['settleAuditRsArea']] : '';
            }
            //总部结算审核状态
            if ($v['settleAuditRole'] == 6){
                $v['settleAuditHeadStr'] = isset($roleList[$v['settleAuditRole']]) ? $roleList[$v['settleAuditRole']].'已审核' : '';
            }
            if ($v['settleAuditHeadStr']){
                $v['settleAuditHeadStr'] .= isset($auditRs[$v['settleAuditRsHead']]) ? ','.$auditRs[$v['settleAuditRsHead']] : '';
            }else{
                $v['settleAuditHeadStr'] = isset($auditRs[$v['settleAuditRsHead']]) ? $auditRs[$v['settleAuditRsHead']] : '';
            }
            $list[$k] = $v;
        }
        return $list;
    }
    /**
     * 更新金额
     * @param number $projectId
     */
    function updateMoney($projectId){
        $findData = $this->getData($projectId);
        $data = array();
        //订单目标达成率
        $data['orderReachRate'] = $findData['orderTarget'] ? (float)round(($findData['orderActual']/$findData['orderTarget'])*100, 2) : 0;
        //留资目标达成率
        $data['capitalReachRate'] = $findData['capitalTarget'] ? (float)round(($findData['capitalActual']/$findData['capitalTarget'])*100, 2) : 0;
        $this->edit($projectId, $data);
    }
    /**
     * 统计对应父级ID的金额
     * @param unknown $procId
     */
    function getMoney($procId){
        $data = array(
            'orderTarget' => 0,//订单目标
            'capitalTarget' => 0,//留资目标
            'orderActual' => 0,//订单实际达成
            'capitalActual' => 0,//留资实际达成
            'budgetApply' => 0,
            'budgetAudit' => 0,
            'settleApply' => 0,
            'settleAudit' => 0,
        );
        $findList = $this->getNewList(array('procId'=>$procId));
        foreach ($findList as $v){
            $data['orderTarget'] += $v['orderTarget'];
            $data['capitalTarget'] += $v['capitalTarget'];
            $data['orderActual'] += $v['orderActual'];
            $data['capitalActual'] += $v['capitalActual'];
            $data['budgetApply'] += $v['budgetApply'];
            in_array($v['budgetAuditRs'], array(1,4)) && $data['budgetAudit'] += $v['budgetAudit'];
            $data['settleApply'] += $v['settleApply'];
            //结算审核金额
            in_array($v['settleAuditRs'], array(1,4)) && $data['settleAudit'] += $v['settleAudit'];
        }
        return $data;
    }
    /**
     * 预算审核状态统计
     * @param int $procId
     * @return multitype:
     */
    function getBudgetStatusCount($procId) {
        $findList = $this->getList(array('procId'=>$procId,'budgetApply > '=>0), null, null, 'budgetAuditRs');
        $count = array();
        foreach ($findList as $v){
            $count[$v['budgetAuditRs']] ++;
        }
        return $count;
    }
    /**
     * 单个预算审核
     * @param number $id
     * @param number $audit
     * @param array $data
     * @return object
     */
    function budgetAudit($id, $audit = 1, $user = array()){
        $findData = $this->getData($id);
        $auditData = array(
            'budgetAudit' =>  $audit==1 ? $findData['budgetApply'] : 0,
            'budgetAuditRs' => $audit,
            'budgetAuditRole' => $user['userRole']
        );
        return $this->db->update($this->table, $auditData,array('id'=>$id));
    }
    /**
     * 结算审核状态统计
     * @param int $procId
     * @return multitype:
     */
    function getSettleStatusCount($procId) {
        $findList = $this->getNewList(array('procId'=>$procId,'settleApply > '=>0), null, null, 'settleAuditRsArea,settleAuditRsHead');
        $count = array();
        foreach ($findList as $v){
            $count[$v['settleAuditRs']] ++;
        }
        return $count;
    }
    /**
     * 单个审核
     * @param number $id
     * @param number $audit
     * @param array $user
     * @return object
     */
    function settleAudit($id, $audit = 1, $user = array()){
        $findData = $this->getData($id);
        $auditData = array(
            'settleAudit' =>  $audit==1 ? $findData['settleApply'] : 0,
            'settleAuditRole' => $user['userRole']
        );
        if ($user['userRole'] == 2){//地区
            $auditData['settleAuditRsArea'] = $audit;
        }elseif ($user['userRole'] == 6){//总部
            $auditData['settleAuditRsHead'] = $audit;
        }
        return $this->db->update($this->table, $auditData,array('id'=>$id));
    }
}